<template>
  <PageWrapper :title="`商品管理`" @back="goBack">
    <BasicTable @register="register">
      <template #toolbar>
        <a-button type="primary" @click="handleNew">新增</a-button>
        <TableAction />
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
  </PageWrapper>
</template>

<script lang="ts">
  import { defineComponent, onActivated } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getBasicColumns } from './productData';
  import { useGo } from '/@/hooks/web/usePage';
  import { PageWrapper } from '/@/components/Page';
  import { useRouter } from 'vue-router';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { delproduct, getProductList } from '/@/api/sys/productList';
  export default defineComponent({
    name: 'DocumentProductManager',
    components: { BasicTable, TableAction, PageWrapper },
    setup() {
      const go = useGo();
      const { currentRoute } = useRouter();
      const { createMessage } = useMessage();
      const { hd_id } = currentRoute.value.query;
      function goBack() {
        go('/document/activity');
      }
      const [register, { reload }] = useTable({
        title: '商品管理',
        api: getProductList,
        searchInfo: { hdid: hd_id },
        columns: getBasicColumns(),
        useSearchForm: false,
        striped: false,
        showIndexColumn: false,
        canResize: false,
        bordered: true,
        beforeFetch(info) {
          if (info && info.id) {
            info.id = info.id[info.id.length - 1];
          }
          return info;
        },
        fetchSetting: {
          pageField: 'pageNum',
          totalField: 'totalCount',
          listField: 'list',
          sizeField: 'pageSize',
        },
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
        rowKey: 'id',
      });
      onActivated(() => {
        const { id } = currentRoute.value.query;
        if (id) {
          reload();
        }
      });
      const hdid = currentRoute.value.query.hd_id;
      function handleNew() {
        go('/document/productEdit?hdid=' + hdid);
      }
      function handleEdit(record: Recordable) {
        go('/document/productEdit?id=' + record.id + '&hdid=' + hdid);
      }
      async function handleDelete(record: Recordable) {
        await delproduct({ id: record.id });
        createMessage.success('删除成功');
        reload();
      }
      return {
        register,
        handleNew,
        handleEdit,
        handleDelete,
        goBack,
      };
    },
  });
</script>
